package com.itheima.reggie.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.bean.Dish;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.service.DishService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: 许志民
 * Date: 2021-11-25
 */
@RestController
@RequestMapping("/dish")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @CacheEvict(value = "CATEGORY_CACHE",key = "'CATEGORY'+#categoryId",allEntries = true)
    @PostMapping
    public R add(@RequestBody DishDto dishDto){
        dishService.saveDishDto(dishDto);
        return R.success("添加成功");
    }
//    dish/page?page=1&pageSize=10
    @GetMapping("/page")
    public R page(Integer page,Integer pageSize,String name){
        Page<DishDto> dtoPage =  dishService.findByPage(page,pageSize,name);
        return R.success(dtoPage);
    }

    /**
     * 修改回显
     * @param id 根据id查询参数
     * @return
     */
    @CacheEvict(value = "CATEGORY_CACHE",key = "'CATEGORY'+#categoryId",allEntries = true)
    @GetMapping("/{id}")
    public R getDish(@PathVariable Long id){
        log.info("取到id为{}",id);
        System.out.println(id);
        DishDto dishDto = dishService.findById(id);
        return R.success(dishDto);
    }
    @CacheEvict(value = "CATEGORY_CACHE",key = "'CATEGORY'+#categoryId",allEntries = true)
    @PutMapping
    public R update(@RequestBody DishDto dishDto){
        boolean falg = dishService.updateBy(dishDto);
        return falg ? R.success("修改成功"):R.error("修改失败");
    }
    @CacheEvict(value = "CATEGORY_CACHE",key = "'CATEGORY'+#categoryId",allEntries = true)
    @PostMapping("/status/{status}")
    public R checkStatus(@PathVariable Integer status,Long[] ids){
        if(status == null && ids == null){
            return R.error("修改失败");
        }
        System.out.println(ids.length);
        boolean falg = dishService.updateStatus(status,ids);

//        return R.success("");
        return falg ? R.success("修改成功"):R.error("修改失败");
    }
    @CacheEvict(value = "CATEGORY_CACHE",key = "'CATEGORY'+#categoryId",allEntries = true)
    @DeleteMapping
    public R deletes(Long[] ids){
        if(ids == null){
            return R.error("删除失败");
        }
        boolean falg = dishService.deleteids(ids);
        return falg ? R.success("删除成功"):R.error("删除失败");
    }
    @Autowired
    private RedisTemplate redisTemplate;
    @Cacheable(value = "CATEGORY_CACHE",key = "'CATEGORY'+#categoryId")
    @GetMapping("/list")
    public R getCategory(Long categoryId){
        List<DishDto> list = dishService.getList(categoryId);
        return R.success(list);
    }

}
